package com.arpa.wms.service.impl;

import com.arpa.wms.domain.enums.DeliverySourceTypeEnum;
import com.arpa.wms.service.IEnumInfoService;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * WMS调拨单明细 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2020-11-17
 */
@Service
public class EnumInfoServiceImpl implements IEnumInfoService {

    /**
     * 获取枚举列表
     *
     * @param type
     * @return
     */
    @Override
    public List<Map<String, Object>> getEnumList(String type) {
        // 1.得到枚举类对象
        Class<DeliverySourceTypeEnum> clz = null;
        try {
            clz = (Class<DeliverySourceTypeEnum>) Class.forName(type);
            // 2.得到所有枚举常量
            Object[] objects = clz.getEnumConstants();
            Method getValue =  clz.getMethod("getValue");
            Method getDesc = clz.getMethod("getDesc");
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            Map<String, Object> map = null;
            for (Object obj : objects) {
                map = new HashMap<String, Object>();
                map.put("code", getValue.invoke(obj));
                map.put("message", getDesc.invoke(obj));
                list.add(map);
            }
            return list;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }
}
